<?php
namespace app\shops\controller;

use think\Db;
use think\Exception;
use think\session;
use think\Model;
use think\response\Json;

/**
 * 学生排课管理
 */
class StudentArrange extends Base
{
    /**
     * @throws
     * 学生课程查看
     * @return [type] [description]
     */
    public function index()
    {
        $stu_id = input('stu_id');

        //拿到该学生加入的集体课程
        $collective_list = Db('stu_many_time')->alias('s')
            ->join('course_time c','s.course_id=c.course_id','left')
            ->join('course co','co.id=s.course_id','left')
            ->where('stu_id',$stu_id)
            ->where('s.is_del',0)
            ->field('s.course_id, s.course_name, c.week_day, c.period_id, c.class_id, co.course_type, c.teacher_id')->select();

        $week_arr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];

        //数据处理
        foreach ($collective_list as $k=>$v){
            if (isset($v['week_day'])){
                $collective_list[$k]['class_name']=Db('classes')->where('id',$v['class_id'])->value('class_name');
                $collective_list[$k]['week_day']=$week_arr[$v['week_day']];
                $collective_list[$k]['teacher_name']=Db('teacher')->where('teacher_id',$v['teacher_id'])->value('teacher_name');
                $period = Db('period')->where('id',$v['period_id'])->find();
                $collective_list[$k]['time'] = $period['start_hour'].":".$period['start_minute']."~".$period['end_hour'].":".$period['end_minute'];
            }else{
                $collective_list[$k]['class_name']="待定";
                $collective_list[$k]['week_day']="暂未排课";
                $collective_list[$k]['teacher_name']="暂未派人";
                $collective_list[$k]['time'] = "";
            }
        }

        //拿到该学生加入的一对多课程
        $many_list = Db('stu_one_time')->alias('s')->join('course c','s.course_id=c.id')
            ->where('stu_id',$stu_id)->where('s.is_del',0)->where('course_type',2)
            ->field('s.course_id, s.week_day, s.period_id, s.class_id, c.course_type, c.course_name, s.teacher_id')->select();
        //数据处理
        foreach ($many_list as $k=>$v){
            $many_list[$k]['class_name']=Db('classes')->where('id',$v['class_id'])->value('class_name');
            $many_list[$k]['teacher_name']=Db('teacher')->where('teacher_id',$v['teacher_id'])->value('teacher_name');
            $many_list[$k]['week_day']=$week_arr[$v['week_day']];
            $period = Db('period')->where('id',$v['period_id'])->find();
            $many_list[$k]['time'] = $period['start_hour'].":".$period['start_minute']."~".$period['end_hour'].":".$period['end_minute'];
        }

        //拿到该学生加入的一对一课程
        $one_list = Db('stu_one_time')->alias('s')->join('course c','s.course_id=c.id')
            ->where('stu_id',$stu_id)->where('s.is_del',0)->where('course_type',3)
            ->field('s.course_id, s.week_day, s.period_id, s.class_id, c.course_type, c.course_name, s.teacher_id')->select();
        //数据处理
        foreach ($one_list as $k=>$v){
            $one_list[$k]['class_name']=Db('classes')->where('id',$v['class_id'])->value('class_name');
            $one_list[$k]['teacher_name']=Db('teacher')->where('teacher_id',$v['teacher_id'])->value('teacher_name');
            $one_list[$k]['week_day']=$week_arr[$v['week_day']];
            $period = Db('period')->where('id',$v['period_id'])->find();
            $one_list[$k]['time'] = $period['start_hour'].":".$period['start_minute']."~".$period['end_hour'].":".$period['end_minute'];
        }

        $this->assign([
            'collective_list' => $collective_list,
            'many_list'       => $many_list,
            'one_list'        => $one_list,
            'stu_id'          => $stu_id
        ]);

        return $this->fetch();

    }

    /**
     * @return mixed
     * 添加集体课程
     */
    public function add()
    {
        $stu_id = input('stu_id');
        // 集体班的添加
        if (request()->isPost()){

            $stu_id = input('stu_id');
            $data['course_arr'] = input('course_arr');
            //处理数据，把json字符串转数组
            $data['course_arr'] = json_decode($data['course_arr'], true);

            //去空处理
            foreach ($data['course_arr'] as $k => $v) {
                if (!$v) {
                    unset($data['course_arr'][$k]);
                }
            }
            //数据处理（不可重复添加相同的课程）
            foreach ($data['course_arr'] as $k => $v) {
                $arr1[] = explode('-', $v);
                foreach ($arr1 as $key => $val) {
                    $courses[$key]['course_id'] = $val[1];
                    $courses[$key]['course_name'] = Db('course')->where('id', $val[1])->value('course_name');
                    $courses[$key]['stu_id'] = $stu_id;
                    $exist_id = Db('stu_many_time')->where('stu_id',$stu_id)->where('course_id',$val[1])->where('is_del',0)->value('id');
                }
            }

            if (!empty($exist_id)){
                $this->error('重复选课~ ');
            }else{
                $res1 = Db('stu_many_time')->insertAll($courses);
                if (!$res1) {
                    $this->error("操作失败");
                }
                //student表中的是否排课状态
                $is_arrange = Db('student')->where('stu_id', $stu_id)->value('is_arrange');
                if ($is_arrange==0){
                    $res2 = Db('student')->where('stu_id', $stu_id)->update(['is_arrange' => 1]);
                    if ($res2){
                        $this->success('选课成功','',$stu_id);
                    }else{
                        $this->error("网络异常");
                    }
                }else{
                    $this->success('选课成功','',$stu_id);
                }
            }

        }

        $store_id = Db('student')->where('stu_id',$stu_id)->value('store_id');

        //列表显示所有课程名称并且类型为集体
        $course_list = Db("course")->where('store_id',$store_id)->where('course_type', 1)->where('status', 0)->field('id as course_id, course_name,course_type')->select();

        //列表显示所有课程名称并且类型为一对多
        $course_list1 = Db("course")->where('store_id',$store_id)->where('course_type', 2)->where('status', 0)->field('id as course_id, course_name, store_id')->select();

        //列表显示所有课程名称并且类型为一对一
        $course_list2 = Db("course")->where('store_id',$store_id)->where('course_type', 3)->where('status', 0)->field('id as course_id, course_name, store_id')->select();

        $this->assign([
            'course_list'   => $course_list,
            'course_list1'  => $course_list1,
            'course_list2'  => $course_list2,
            'stu_id'        => $stu_id
        ]);
        return $this->fetch();

    }

    /**
     * 学生添加一对多课程
     */
    public function add_one()
    {

        $stu_id    = input('stu_id');
        $course_id = input('course_id');
        $store_id  = input('store_id');

        if(request()->isAjax()) {

            $course_id = input('course_id');
            $stu_id = input('stu_id');
            $data['arrange_time'] = input('arrange_time');
            //处理数据，把json字符串转数组
            $data['arrange_time'] = json_decode($data['arrange_time'], true);


            //数据处理，分割星期和时间段
            foreach ($data['arrange_time'] as $k => $v) {
                $arr = explode('&', $v);
                $week_data['week_day']  = $arr[0];
                $week_data['period_id'] = $arr[1];
                $week_data['class_id']  = $arr[2];
                $week_data['teacher_id']= $arr[3];
                $week_data['course_id'] = $course_id;
                $week_data['stu_id'] = $stu_id;
                $exist_id = Db('stu_one_time')->where('stu_id', $stu_id)->where('course_id', $course_id)
                    ->where('week_day',$arr[0])->where('period_id',$arr[1])->where('is_del',0)->value('id');

                if (!empty($exist_id)) {
                    $this->error('重复选课~');
                }

                $res1 = Db('stu_one_time')->insert($week_data);
                if (!$res1) {
                    $this->error("操作失败");
                }
            }

            $is_arrange = Db('student')->where('stu_id', $stu_id)->value('is_arrange');
            if ($is_arrange==0){
                $res2 = Db('student')->where('stu_id', $stu_id)->update(['is_arrange' => 1]);
                if ($res2){
                    $this->success('选课成功','',$stu_id);
                }else{
                    $this->error("网络异常");
                }
            }else{
                $this->success('选课成功','',$stu_id);
            }
        }

        //拿到该课程名称
        $course_info = Db("course")->where('id',$course_id)->field('id as course_id, course_name')->find();

        //拿到该门店下的所有班级
        $classes_list = Db('classes')->where('shop_id',$this->shop_id)->where('store_id',$store_id)->field('id as class_id, class_name')->where('status',0)->select();

        //该数组展示星期
        $week_arr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];

        // 列表显示所有排课时段
        $period_list = Db("period")->where('shop_id',$this->shop_id)->where('is_show',1)->order("start_hour asc")->select();
        // 排课时段数据处理
        if ($period_list){
            foreach ($period_list as $k=>$v){
                $period_list[$k]['time']   = $v['start_hour'].":".$v['start_minute']."~".$v['end_hour'].":".$v['end_minute'];
            }
        }

        //列表显示所有该门店的教师
        $teacher_list = Db("teacher")->where('shop_id',$this->shop_id)->where('store_id',$store_id)->field('teacher_id, teacher_name')->select();

        $this->assign([
            'course_info'  => $course_info,
            'classes_list' => $classes_list,
            'week_arr'     => $week_arr,
            'period_list'  => $period_list,
            'teacher_list' => $teacher_list,
            'stu_id'       => $stu_id
        ]);


        return $this->fetch();
    }


    /**
     * 删除课程
     */
    public function del()
    {
        $stu_id = input('stu_id');

        if (request()->isPost()){

            $stu_id  = input('stu_id');
            $data    = input('course_arr');

            //处理数据，把json字符串转数组
            $data = json_decode($data, true);

            //去空
           foreach ($data as $k => $v) {
                if (!$v) {
                    unset($data[$k]);
                }
            }

            //判断类型，分别删除
            foreach ($data as $k => $v) {
                $data[$k] = explode(',',$v);

                $course_type = Db('course')->where('id',$data[$k][0])->value('course_type');
                if ($course_type==1){
                    $res = Db('stu_many_time')->where('stu_id',$stu_id)->where('course_id',$data[$k][0])->update(['is_del'=>1]);
                }else{
                    $res = Db('stu_one_time')->where('stu_id',$stu_id)->where('course_id',$data[$k][0])->update(['is_del'=>1]);
                }

               if (!$res){
                    $this->error('删除失败');
                }

            }


            $res1 = Db('stu_many_time')->where('stu_id',$stu_id)->where('is_del',0)->value('stu_id');
            $res2 = Db('stu_one_time')->where('stu_id',$stu_id)->where('is_del',0)->value('stu_id');

            if ($res1==''&&$res2==''){
                $res3 = Db('student')->where('stu_id',$stu_id)->update(['is_arrange'=>0]);
                if ($res3){
                    $this->success('删除成功','',['stu_id'=>$stu_id,'flag'=>0]);
                }else{
                    $this->error('网络异常');
                }

            }
            $this->success('删除成功','',['stu_id'=>$stu_id,'flag'=>1]);

        }


        //集体数据
        $course1 = Db('stu_many_time')->alias('s')->join('course c','s.course_id=c.id')->join('course_time ct','ct.course_id=s.course_id')
            ->where('stu_id',$stu_id)->where('s.is_del',0)->field('s.course_id,s.course_name,course_type,ct.teacher_id')->select();
        //数据处理
        foreach ($course1 as $k=>$v){
            $course1[$k]['teacher_name'] = Db('teacher')->where('teacher_id',$v['teacher_id'])->value('teacher_name');
        }

        //一对多或一对一数据
        $course2 = Db('stu_one_time')->alias('s')->join('course c','s.course_id=c.id')
            ->where('stu_id',$stu_id)->where('s.is_del',0)->field('s.course_id,c.course_name, c.course_type,s.teacher_id')->select();

        //数据处理
        foreach ($course2 as $k=>$v){
            $course2[$k]['teacher_name'] = Db('teacher')->where('teacher_id',$v['teacher_id'])->value('teacher_name');
        }

        $this->assign([
            'course1' => $course1,
            'course2' => $course2,
            'stu_id'  => $stu_id
        ]);

        return $this->fetch();
    }


}
